
import trees.settings.Settings
import trees.parsers.{NewickParser, NexusParser, TreeData}
import trees.tree.{TreeBuilder,IdFactory, AbstractTree, ImmutableTree}
import scala.collection.mutable.HashMap

object Main {

	def main(args: Array[String]){

		//Test of newick parser, reading a single tree from a newick file into TreeData format
		val newickData = NewickParser.readNewickStringFromFile(args(0))
		//println("Tree string read from " + args(0) +" and cleaned using NewickParser.readNewickStringFromFile: " +newickData.treeString)

                val immutableTree = TreeBuilder.buildImmutableTree(newickData(0))
                var buildTreeString = immutableTree.toString()

                if (newickData(0).treeString.equals(buildTreeString))
                    println("Good news, the input string and the build string are identical \n")


//                if (newickData.treeString.equals(buildTreeString))
//                    println("Good news, the input string and the build string are identical \n")

                val reRooted=immutableTree.reRoot(IdFactory.getID("BJ"))
                println("Before:   " +immutableTree.toString)
                println("Rerooted: " +reRooted)
                println(reRooted.treeList+"\n"+reRooted.idList)




                //----------------------------------------------------------------------
                //Testing nexus parser and treebuilder performance by reading 90001 trees from file
                Settings.verbose = 2
                var treeList = NexusParser.getImmutableTreesFromFile(args(2))
                println("Parsed and created " +treeList.length +" trees from file")

                val map = new HashMap[AbstractTree, Int]
                var max = 0
                map.update(treeList head, 1)
                for (tree <- treeList tail){
                    if (map.contains(tree)){
                        map.update(tree, map(tree)+1)
                        if (map(tree) > max ){
                            max = map(tree)
                        }
                    }else{
                        map.update(tree, 1)
                    }
                }

                println("No of unique trees: " +map.keySet.size)
                println("Cleared identify identical trees test")

                println("Writing to file")
                NexusParser.writeNexusFile("trees/hugeNoWeights.nex", map.keys.toList)
                println("Wrote nexus without weights")
                NexusParser.writeNexusFileWithWeights("trees/hugeWeighted.nex", map.keys.toList, map.values.toList.map(e => max/e))
                                                      
                println("Wrote nexus with weights")

	}
}
